using System;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Unity;
using Silverlight.Weblog.Client.DAL.Events;
using Silverlight.Weblog.Client.DAL.ViewModel;
using Silverlight.Weblog.Common.IoC;

namespace Silverlight.Weblog.Client.Default.Widgets
{
    public class SecondaryHeadlineWidgetViewModel : ViewModelBase, ISecondaryHeadlineWidgetViewModel, IDisposable
    {
        public SecondaryHeadlineWidgetViewModel()
        {
            IoC.Get<IEventAggregator>().GetEvent<ChangingTitleEvent>().Subscribe(ChangingTitle);
        }

        public void ChangingTitle(string str)
        {
            Title = str;

            UpdatevisualState();

            ScrollToService.ScrollToTop();
        }

        [Dependency]
        public IScrollToTop ScrollToService { get; set; }

        private void UpdatevisualState()
        {
            if (!string.IsNullOrEmpty(Title))
                GoToState("Expanded");
            else
                GoToState("Collapsed");
        }

        #region Title
        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                if (_title != value)
                {
                    _title = value;
                    RaisePropertyChanged("Title");
                }

            }
        }
        #endregion Title

        #region Implementation of IDisposable

        public void Dispose()
        {
            IoC.Get<IEventAggregator>().GetEvent<ChangingTitleEvent>().Unsubscribe(ChangingTitle);
        }

        #endregion
    }
}